<?php

namespace App\Http\Controllers\Admin\Anchor;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Anchor\AnchorWorksRepository;
use App\Rules\AnchorWorksValidate;
use Illuminate\Http\Request;

// 主播
class WorksController extends BaseController
{
    private $repoist;

    public function __construct(AnchorWorksRepository $repository)
    {
        $this->repoist = $repository;
    }

    public function index(Request $request)
    {

        $data = $this->repoist->index($request->all(), $request->get('pageNum'), $request->get('pageSize'));
        $data['statusList'] = config('card.anchorWorksStatus');
        return $data;
    }

    public function logs(Request $request)
    {
        $where = [];
        if ($request->has('dateRange') && !empty($request->get('dateRange'))) {
            $time = $request->get('dateRange');
            $where['between'] = ['created_at' => [$time]];
        }
        $data = $this->repoist->logs($where, $request->get('pageNum'), $request->get('pageSize'));
        $data['statusList'] = config('card.anchorWorksStatus');
        return $data;
    }

//    设置推广费用
    public function cost(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) {
            return $this->returnMsg(1, '数据不对');
        }
        return $this->repoist->setCost($request->all());
    }

//下播操作
    public function offLine(Request $request)
    {
        $id = (int)$request->get('id');
        if ($id <= 0) {
            return $this->returnMsg(1, '达人数据不对');
        }
        $this->repoist->offLine($id);
        return $this->returnMsg(200, '操作成功完成');
    }

//开播
    public function add(Request $request)
    {
        $Validator = new AnchorWorksValidate();
        if (!$Validator->check($request->all())) {
            return $this->returnMsg(1, $Validator->getError());
        }


        return $this->repoist->add($request);
    }

//修改即换播
    public function edit(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $Validator = new AnchorWorksValidate();
        if (!$Validator->scene('edit')->check($request->all())) {
            return $this->returnMsg(1, $Validator->getError());
        }
        return $this->repoist->exchange($request);
    }

//    修改时间
    public function editTimes(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        return $this->repoist->editTimes($request);
    }

    public function del(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        return $this->repoist->del($id, $request->user->shop_id);
    }
}
